package com.doudizu.shoesonline.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.doudizu.shoesonline.common.BaseResponse;
import com.doudizu.shoesonline.common.ErrorCode;
import com.doudizu.shoesonline.common.ResultUtils;
import com.doudizu.shoesonline.exception.BusinessException;
import com.doudizu.shoesonline.model.dto.order.OrderDTO;
import com.doudizu.shoesonline.model.dto.order.OrderQuery;
import com.doudizu.shoesonline.model.entity.OrderDetail;
import com.doudizu.shoesonline.model.entity.Orders;
import com.doudizu.shoesonline.model.entity.Product;
import com.doudizu.shoesonline.service.OrderDetailService;
import com.doudizu.shoesonline.service.OrdersService;
import com.doudizu.shoesonline.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author ln
 */
@RestController
@RequestMapping("/order")
@Api(tags = "订单模块")
@Slf4j
public class OrderController {

    @Resource
    private OrdersService ordersService;

    @Resource
    private ProductService productService;

    @Resource
    private OrderDetailService orderDetailService;

    @GetMapping("/list")
    @ApiOperation("分页查询")
    public BaseResponse list(Integer pageNum, Integer pageSize,Integer status) {
        return ResultUtils.success(ordersService.getOrders(pageNum,pageSize,status));
    }


    @GetMapping("/status")
    @ApiOperation("根据status获取所有订单")
    public BaseResponse status(OrderQuery orderQuery) {
        return ResultUtils.success(ordersService.getOrdersVOs(orderQuery));
    }

    @GetMapping("/details")
    @ApiOperation("获取订单详情")
    public BaseResponse details(String id) {
        return ResultUtils.success(ordersService.getOrderDetails(id));
    }


    @PostMapping("/submit")
    @ApiOperation("提交订单")
    public BaseResponse add(@RequestBody OrderDTO orderDTO) {
        List<OrderDetail> orderDetails = ordersService.submit(orderDTO);
        return ResultUtils.success(orderDetails);
    }

    @PostMapping("/pay")
    @ApiOperation("支付")
    public BaseResponse pay(String id,String payType) {
        try {
            //TODO 库存
            Orders order = new Orders();
            order.setId(Long.valueOf(id));
            order.setStatus(2);
            order.setPayType(Integer.valueOf(payType));
            order.setPayTime(new Date());
            ordersService.updateById(order);
        } catch (Exception e) {
            log.error("pay error",e);
            throw new BusinessException(ErrorCode.OPERATION_ERROR,"支付失败");
        }
        return ResultUtils.success(null);
    }

    @PostMapping("/send")
    @ApiOperation("发货")
    public BaseResponse send(Long id) {
        try {
            Orders order = new Orders();
            order.setId(id);
            order.setStatus(3);
            ordersService.updateById(order);
        } catch (Exception e) {
            log.error("send error",e);
            throw new BusinessException(ErrorCode.OPERATION_ERROR,"发货失败");
        }
        return ResultUtils.success(null);
    }

    @PostMapping("/finish")
    @ApiOperation("完成订单")
    @Transactional
    public BaseResponse finish(Long id) {
        try {
            Orders order = new Orders();
            order.setId(id);
            order.setStatus(4);
            order.setFinishTime(new Date());
            ordersService.updateById(order);
            QueryWrapper<OrderDetail> orderDetailQueryWrapper = new QueryWrapper<>();
            orderDetailQueryWrapper.eq("order_id",id);
            List<OrderDetail> orderDetailList = orderDetailService.list(orderDetailQueryWrapper);
            for (OrderDetail orderDetail : orderDetailList) {
                Integer productId = orderDetail.getProductId();
                Product product = productService.getById(productId);
                product.setSales(product.getSales() + orderDetail.getNum());
                product.setStock(product.getStock() - orderDetail.getNum());
                productService.updateById(product);
            }

        } catch (Exception e) {
            log.error("finish error",e);
            throw new BusinessException(ErrorCode.OPERATION_ERROR,"完成订单失败");
        }
        return ResultUtils.success(null);
    }

    @PostMapping("/apply")
    @ApiOperation("申请退货")
    public BaseResponse apply(Long id) {
        try {
            Orders order = new Orders();
            order.setId(id);
            order.setStatus(5);
            ordersService.updateById(order);
        } catch (Exception e) {
            log.error("apply error",e);
            throw new BusinessException(ErrorCode.OPERATION_ERROR,"申请退货失败");
        }
        return ResultUtils.success(null);
    }

    @PostMapping("/verify")
    @ApiOperation(value = "审核退货",notes = "i=0拒绝，i=1同意")
    public BaseResponse verify(Long id,Integer i) {
        try {
            //TODO 库存
            Orders order = new Orders();
            order.setId(id);
            if (i==1) {
                order.setStatus(6);
            }
            ordersService.updateById(order);
        } catch (Exception e) {
            log.error("apply error",e);
            throw new BusinessException(ErrorCode.OPERATION_ERROR,"退货失败");
        }
        return ResultUtils.success(null);
    }

}
